package com.be.beadmin.design.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.be.beadmin.common.entity.PageEntity;
import com.be.beadmin.common.utils.ResultModel;
import com.be.beadmin.design.dto.input.BopInfoDto;
import com.be.beadmin.design.dto.output.BopInfoEx;
import com.be.beadmin.design.service.BopInfoService;
import com.be.beadmin.logging.annotation.Log;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

/**
* @author Jiahao Liu
* @date 2023-02-22
**/
@Api(tags = {"设计管理-工艺路径序列"})
@RestController
public class BopInfoController {

    @Autowired
    private  BopInfoService bopInfoService;
    @Log
    @PostMapping(value = "/design/getBopInfoByPage")
    public ResultModel getBopInfoByPage(@RequestBody PageEntity pageEntity)  {
        return ResultModel.success(bopInfoService.getBopInfoByPage(pageEntity)) ;
    }

    @PostMapping("/design/addBopInfo")
    public ResultModel addBopInfo(@RequestBody BopInfoDto bopInfoDto){
        bopInfoService.addBopInfo(bopInfoDto);
        return ResultModel.success(ResultModel.ADD);
    }

    @PostMapping("/design/delBopInfo")
    public ResultModel delBopInfo(@RequestBody BopInfoDto bopInfoDto){
        bopInfoService.delBopInfo(bopInfoDto.getId());
        return ResultModel.success(ResultModel.DELETE);
    }

    @PostMapping("/design/updateBopInfo")
    public ResultModel updateBopInfo(@RequestBody BopInfoDto bopInfoDto){
        bopInfoService.updateBopInfo(bopInfoDto);
        return ResultModel.success(ResultModel.UPDATE);
    }

    @PostMapping("/design/exportBopInfo")
    public void exportBopInfo(HttpServletResponse response ,@RequestBody PageEntity pageEntity) throws IOException {
        Page<BopInfoEx> bopInfoPage = bopInfoService.getBopInfoByPage(pageEntity);
        List<BopInfoEx> bopInfoExes = bopInfoPage.getContent();
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("BopInfo接口", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xls");
        //EasyExcel.write(response.getOutputStream(), BopInfoEx.class).sheet("sheet1").doWrite(bopInfoExes);
        ExcelWriterBuilder ewb=EasyExcel.write(response.getOutputStream(), BopInfoEx.class);
        ewb.excelType(ExcelTypeEnum.XLS);
        ewb.sheet("sheet1").doWrite(bopInfoExes);
    }
}